<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Activity;
use app\index\model\Users;
/*图片上传类*/
class Picture extends Common
{  

  //上传用户头像
  public function userUpload(){
  	  $userPhone=input('userPhone','');//手机号
      $userPhoto = request()->file('userPhoto');//用户头像
  	  $userPhoto = request()->file('userImg1');//
  	  if(empty($userPhoto) || empty($userPhoto)){
  	  	return json(['state'=>'203','msg'=>'参数不足']);
  	  }
  	  $rs=Db::name('user_info')->where(['userPhone'=>$userPhone])->find();
  	  if($rs){
  	  	return json(['state'=>'204','msg'=>'手机号已经注册过啦']);
  	  }
  	  if(!empty($userPhoto)){
            $rs=$this->uploadImg($userPhoto);
            if($rs){
               $captainPhoto=$rs;//领队头像
            }else{
                return json(['state' => '202', 'msg' => '用户头像上传失败']);
            }
        }
        $sdata=['userPhoto'=>$userPhoto,'userPhone'=>$userPhone];
        $re=Db::name('user_info')->add($sdata);
        if($re){
        	return json(['state'=>'200','msg'=>'添加成功']);
        }else{
           return json(['state'=>'201','msg'=>'添加失败再试试']);
        }

  }
  //投诉上传图片
  public function UploadComplaint(){
    $uid=input('uid');
    $nearby_id=input('nearby_id');
    $Img= request()->file('Img');//
    if(empty($uid) || empty($nearby_id) || empty($Img)){
      return json(['state'=>'203','msg'=>'参数不足']);
    }
    if(!empty($Img)){
            $rs=$this->uploadImg($Img);
            if($rs){
               $Img=$rs;//投诉图片
            }else{
                return json(['state' => '202', 'msg' => '投诉图片上传失败']);
            }
        }
       $sdata=['Img'=>$Img];
        $re=Db::name('complaint')->where(['uid'=>$uid,'nearby_id'=>$nearby_id,'state'=>'0'])->update($sdata); 
        if($re){
          return json(['state'=>'200','msg'=>'上传成功']);
        }else{
          return json(['state'=>'201','msg'=>'数据没改动']);
        } 
    
  }
  //个人编辑图片上传接口
  public function  uploadSet(){
  	   $uid=input('uid');//用户id
  	   $userInfo = Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->find();
      // print_r($userInfo);die;
  	   if(empty($userInfo)){
            return json(['state'=>'202','msg'=>'用户已不存在']);
  	   }
  	   $userPhoto = request()->file('userPhoto');//用户头像
        $file1 = request()->file('userImg1');//背景图
        $file2 = request()->file('userImg2');
        $file3 = request()->file('userImg3');
    	$file4 = request()->file('userImg4');
    	$file5 = request()->file('userImg5');
    	$file6 = request()->file('userImg6');
        if(!empty($userPhoto)){
            $rs=$this->uploadImg($userPhoto);
            if($rs){
               $userPhoto=$rs;//领队头像
             $headers=$this->setHeader();
             $accid=$userInfo['accid'];
             if(!empty($accid)){
              //修改网易云用户头像
              $post_data=['accid'=>$accid,'icon'=>$userPhoto];
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt($ch, CURLOPT_URL,'http://api.netease.im/nimserver/user/updateUinfo.action');
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
              $output = curl_exec($ch);  
             }
            }else{
                $userPhoto="";
                return json(['state' => '203', 'msg' => '用户头像上传失败']);
            }
        }else{
        	//用户头像
        	$userPhoto=$userInfo['userPhoto'];
           // $userPhoto = input('userPhoto');
        }
        if(!empty($file1)){
            $rs=$this->uploadImg($file1);
            if($rs){
               $img1=$rs;//背景图1
            }else{
                return json(['state' => '203', 'msg' => '背景图1上传失败']);
            }
        }else{
        	$img1=$userInfo['userImg1'];
        }
          if(!empty($file2)){
            $rs=$this->uploadImg($file2);
            if($rs){
               $img2=$rs;//背景图2
            }else{
                return json(['state' => '203', 'msg' => '背景图2上传失败']);
            }
        }else{
        	$img2=$userInfo['userImg2'];
        }
          if(!empty($file3)){
            $rs=$this->uploadImg($file3);
            if($rs){
               $img3=$rs;//背景图2
            }else{
                return json(['state' => '203', 'msg' => '背景图3上传失败']);
            }
        }else{
        	$img3=$userInfo['userImg3'];
        }
          if(!empty($file4)){
            $rs=$this->uploadImg($file4);
            if($rs){
               $img4=$rs;//背景图2
            }else{
                return json(['state' => '203', 'msg' => '背景图4上传失败']);
            }
        }else{
        	$img4=$userInfo['userImg4'];
        }
          if(!empty($file5)){
            $rs=$this->uploadImg($file5);
            if($rs){
               $img5=$rs;//背景图2
            }else{
                return json(['state' => '203', 'msg' => '背景图5上传失败']);
            }
        }else{
        	$img5=$userInfo['userImg5'];
        }
        if(!empty($file6)){
            $rs=$this->uploadImg($file6);
            if($rs){
               $img6=$rs;//背景图2
            }else{
                return json(['state' => '203', 'msg' => '背景图6上传失败']);
            }
        }else{
        	$img6=$userInfo['userImg6'];
        }
        $data=['userPhoto'=>$userPhoto,
        'userImg1'=>$img1,'userImg2'=>$img2,'userImg3'=>$img3,
        'userImg4'=>$img4,'userImg5'=>$img5,'userImg6'=>$img6,'user_audit'=>'-1'];
    	$result = Db::name('user_info')->where(['uid'=>$uid])->update($data);
    	if($result){
    		 return json(['state' => '200', 'msg' => '图片修改成功']);
    	}else{
             return json(['state' => '201', 'msg' => '图片修改失败']);
        }

  }
}